package io.scnb.lesson003.demo1;

import io.scnb.lesson003.demo1.test0.userregister.MainConfig0;
import io.scnb.lesson003.demo1.test0.userregister.UserRegisterService;
import io.scnb.lesson003.demo1.test1.OrderCreateEvent;
import io.scnb.lesson003.demo1.test1.SendEmailOnOrderCreateListener;
import io.scnb.lesson003.demo1.test2.MainConfig2;
import io.scnb.lesson003.demo1.test3.MainConfig3;
import io.scnb.lesson003.demo1.test4.MainConfig4;
import io.scnb.lesson003.demo1.test5.MainConfig5;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.SimpleApplicationEventMulticaster;

public class EventTest {
    
    @Test
    public void test0() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig0.class);
        // 获取用户注册服务
        UserRegisterService userRegisterService = context.getBean(UserRegisterService.class);
        // 模拟注册用户
        userRegisterService.registerUser("scnb");
    }
    
    @Test
    public void test1() {
        // 创建事件广播器
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
        // 注册事件监听器
        simpleApplicationEventMulticaster.addApplicationListener(new SendEmailOnOrderCreateListener());
        // 广播事件-订单创建成功事件
        simpleApplicationEventMulticaster.multicastEvent(new OrderCreateEvent(this, 0001L));
    }
    
    @Test
    public void test2() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
        io.scnb.lesson003.demo1.test2.UserRegisterService userRegisterService = context.getBean(io.scnb.lesson003.demo1.test2.UserRegisterService.class);
        userRegisterService.registerUser("scnb");
    }

    @Test
    public void test3() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig3.class);
        io.scnb.lesson003.demo1.test3.UserRegisterService userRegisterService = context.getBean(io.scnb.lesson003.demo1.test3.UserRegisterService.class);
        userRegisterService.registerUser("scnb");
    }

    @Test
    public void test4() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig4.class);
        io.scnb.lesson003.demo1.test4.UserRegisterService userRegisterService = context.getBean(io.scnb.lesson003.demo1.test4.UserRegisterService.class);
        userRegisterService.registerUser("scnb");
    }

    @Test
    public void test5() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig5.class);
        io.scnb.lesson003.demo1.test5.UserRegisterService userRegisterService = context.getBean(io.scnb.lesson003.demo1.test5.UserRegisterService.class);
        userRegisterService.registerUser("scnb");
    }
}   
